<?php

include_once ('bn_path.php');

class bn_images
{
    public $new_width;
    public $new_height;
    public $rotat = 0;
    public $with_quality = false;
    protected static $instance;
    private $conf;
    
    function __construct()
    {
       $this->conf=bn_config::getInstance(); 
    }

    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function Create_Thumbnil($imgfile, $savefile)
    {
        if (file_exists($imgfile))
        {
            $ext = strtolower(substr($imgfile, -3));
            $image = null;
            if ($ext == 'jpg' || $ext == 'jpeg')
                $image = imagecreatefromjpeg($imgfile);
            if ($ext == 'png')
                $image = imagecreatefrompng($imgfile);
            if ($ext == 'gif')
                $image = imagecreatefromgif($imgfile);
            if ($ext == 'bmp' || $ext == 'wbmp')
                $image = imagecreatefromwbmp($imgfile);
            if ($image != null)
            {
                if (!isset($this->new_width))
                    $this->new_width = imagesx($image);
                if (!isset($this->new_height))
                    $this->new_height = imagesy($image);

                $tempimg = imagecreatetruecolor($this->new_width, $this->new_height);

                if ($this->with_quality)
                    imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $this->new_width, $this->new_height, imagesx($image), imagesy($image));
                else
                    imagecopyresized($tempimg, $image, 0, 0, 0, 0, $this->new_width, $this->new_height, imagesx($image), imagesy($image));
                    
                if(is_dir($savefile))
                $savefile.=basename($imgfile); 
                echo "$imgfile\n $savefile" ;  

                $ext = strtolower(substr($savefile, -3));
                if ($ext == 'jpg' || $ext == 'jpeg')
                    imagejpeg($tempimg, $savefile, 90);
                if ($ext == 'png')
                    imagepng($tempimg, $savefile);
                if ($ext == 'gif')
                    imagegif($tempimg, $savefile);
                if ($ext == 'bmp' || $ext == 'wbmp')
                    imagewbmp($tempimg, $savefile);
                imagedestroy($image);
                imagedestroy($tempimg);
                return 1;

            } else
                return 2;
        } else
            return 3;
    }

    public function rgb2int($rgb)
    {
        $hex = "0x";
        $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
        $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

        return hexdec($hex);
    }

    public function add_watermark($imgfile, $savefile, $text, $textX, $textY, $font ='', $fontsize = 0, $color = array(255,0,0))
    {
        if (file_exists($imgfile))
        {
            $ext = strtolower(substr($imgfile, -3));
            $image = null;
            if ($ext == 'jpg' || $ext == 'jpeg')
                $image = imagecreatefromjpeg($imgfile);
            if ($ext == 'png')
                $image = imagecreatefrompng($imgfile);
            if ($ext == 'gif')
                $image = imagecreatefromgif($imgfile);
            if ($ext == 'bmp' || $ext == 'wbmp')
                $image = imagecreatefromwbmp($imgfile);
            if ($image != null)
            {
                list($width, $height) = getimagesize($imgfile);
                if (!isset($this->new_width))
                    $this->new_width = $width;
                if (!isset($this->new_height))
                    $this->new_height = $height;
                $tempimg = imagecreatetruecolor($this->new_width, $this->new_height);

                $color = self::rgb2int($color);
                if ($this->with_quality)
                    imagecopyresampled($tempimg, $image, 0, 0, 0, 0, $this->new_width, $this->
                        new_height, $width, $height);
                else
                    imagecopyresized($tempimg, $image, 0, 0, 0, 0, $this->new_width, $this->
                        new_height, $width, $height);

                if (is_file($font) && $fontsize > 0)
                    imagettftext($tempimg, $fontsize, $this->rotat, $textX, $textY, $color, $font, $text);
                else
                    ImageString($tempimg, 10, $textX, $textY, $text, $color);

                $ext = strtolower(substr($savefile, -3));
                if ($ext == 'jpg' || $ext == 'jpeg')
                    imagejpeg($tempimg, $savefile, 90);
                if ($ext == 'png')
                    imagepng($tempimg, $savefile);
                if ($ext == 'gif')
                    imagegif($tempimg, $savefile);
                if ($ext == 'bmp' || $ext == 'wbmp')
                    imagewbmp($tempimg, $savefile);
                imagedestroy($image);
                imagedestroy($tempimg);
                return 1;
            } else
                return 2;
        } else
            return 3;
    }

    public function rotate_image($imgfile, $savefile, $color = array(255,255,255))
    {
        if (file_exists($imgfile))
        {
            $ext = strtolower(substr($imgfile, -3));
            $image = null;
            if ($ext == 'jpg' || $ext == 'jpeg')
                $image = imagecreatefromjpeg($imgfile);
            if ($ext == 'png')
                $image = imagecreatefrompng($imgfile);
            if ($ext == 'gif')
                $image = imagecreatefromgif($imgfile);
            if ($ext == 'bmp' || $ext == 'wbmp')
                $image = imagecreatefromwbmp($imgfile);
            if ($image != null)
            {
                list($width, $height) = getimagesize($imgfile);
                if (!isset($this->new_width))
                    $this->new_width = $width;
                if (!isset($this->new_height))
                    $this->new_height = $height;
                $tempimg = imagecreatetruecolor($this->new_width, $this->new_height);
                $color = self::rgb2int($color);
                $tempimg = imagerotate($image, $this->rotat, $color);

                $ext = strtolower(substr($savefile, -3));
                if ($ext == 'jpg' || $ext == 'jpeg')
                    imagejpeg($tempimg, $savefile, 90);
                if ($ext == 'png')
                    imagepng($tempimg, $savefile);
                if ($ext == 'gif')
                    imagegif($tempimg, $savefile);
                if ($ext == 'bmp' || $ext == 'wbmp')
                    imagewbmp($tempimg, $savefile);
                imagedestroy($image);
                imagedestroy($tempimg);
                return 1;
            } else
                return 2;
        } else
            return 3;
    }
    
    public function small_thumbnil($filename)
    {
        $filename=bn_dir_files('upload/'.basename($filename));
        $small=$this->conf->Get_value('smalthumb');
        $small=explode('*',$small);
        $this->new_width=$small[0];
        $this->new_height=$small[1];
        self::Create_Thumbnil($filename,bn_dir_files('/thumbnil/smalthumb/'));
    }
    
    public function large_thumbnil($filename)
    {
        $filename=bn_dir_files('upload/'.basename($filename));
        $small=$this->conf->Get_value('largthumb');
        $small=explode('*',$small);
        $this->new_width=$small[0];
        $this->new_height=$small[1];
        self::Create_Thumbnil($filename,bn_dir_files('/thumbnil/largthumb/'));
    } 
}

?>